﻿/**
 * 
 * Copyright 2009 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using mooseirc.utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace mooseirc
{
    /// <summary>
    /// Used to manage settings
    /// </summary>
    public static class Settings
    {

        /// <summary>
        /// Settings Data Structure
        /// </summary>
        private static Dictionary<string, Dictionary<string, string>> m_dictSettings;

        /// <summary>
        /// initializes settings static members
        /// </summary>
        static Settings()
        {
            importSettings(false); //Import all settings, specifying no/merge, this will initialize the settings structure for the first time.
        }

        /// <summary>
        /// Gets the Settings Structure
        /// </summary>
        public static Dictionary<string, Dictionary<string, string>> SettingsStructure
        {
            get
            {
                return m_dictSettings;
            }
        }

        /// <summary>
        /// Reset Settings Structure, Fresh Pull
        /// </summary>
        public static void RefreshSettings()
        {
            importSettings(false);
            return;
        }

        /// <summary>
        /// Update Settings Structure, merge, (Optimized)
        /// This will pull the settings again, and simply update existing structures,
        /// be careful with this...
        /// </summary>
        public static void UpdateSettings()
        {
            importSettings(true);
            return;
        }

        /// <summary>
        /// Import all conifuration files
        /// </summary>
        private static void importSettings(bool merge)
        {
            if (!merge) m_dictSettings = new Dictionary<string, Dictionary<string, string>>();
            try
            {
                foreach (string filePath in Directory.GetFiles(Environment.CurrentDirectory, "*.ini"))
                    importSettingsFile(filePath);
            }
            catch (Exception caught)
            {
                Console.Error.WriteLine(caught.Message);
                Console.Error.WriteLine(caught.StackTrace);
            }
            return;
        }

        /// <summary>
        /// Import a single settings file
        /// </summary>
        /// <param name="filePath">new file path</param>
        private static void importSettingsFile(string filePath)
        {
            try
            {
                Dictionary<string, Dictionary<string, string>> settings = INIUtility.ReadAll(filePath);
                foreach (string group in settings.Keys)
                {
                    if (!m_dictSettings.ContainsKey(group)) m_dictSettings[group] = new Dictionary<string, string>();
                    foreach (string setting in settings[group].Keys)
                        m_dictSettings[group][setting] = settings[group][setting];
                }
            }
            catch (Exception caught)
            {
                Console.Error.WriteLine(caught.Message);
                Console.Error.WriteLine(caught.StackTrace);
            }
            return;
        }

        

    }
}
